package com.example.gaoyang.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.gaoyang.domain.JointCar;
import com.example.gaoyang.domain.request.JointCarRequest;
import com.example.gaoyang.domain.vo.JointCarVO;
import com.example.gaoyang.enums.JointCarTypeEnum;
import com.example.gaoyang.mapper.JointCarMapper;
import com.example.gaoyang.service.JointCarService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Slf4j
@Service
@Transactional
public class JointCarServiceImpl extends ServiceImpl<JointCarMapper, JointCar> implements JointCarService {

    @Override
    public List<JointCarVO> getJointCarList(JointCarRequest jointCarRequest) {
        LambdaQueryWrapper<JointCar> wrapper = this.createWrapper(jointCarRequest);
        List<JointCarVO> list = BeanUtil.copyToList(this.list(wrapper), JointCarVO.class);
        // 赋值车辆类型
        list.forEach(item -> {
            // 车辆类型
            String carType = item.getCarType();
            item.setCarTypeName(JointCarTypeEnum.getNameByType(carType));
        });
        return list;
    }

    private LambdaQueryWrapper<JointCar> createWrapper(JointCarRequest jointCarRequest) {
        return new LambdaQueryWrapper<JointCar>()
                .eq(jointCarRequest.getJointDate() != null, JointCar::getJointDate, jointCarRequest.getJointDate())
                .eq(StringUtils.isNotEmpty(jointCarRequest.getCarType()), JointCar::getCarType, jointCarRequest.getCarType())
                .eq(StringUtils.isNotEmpty(jointCarRequest.getLicensePlate()), JointCar::getLicensePlate, jointCarRequest.getLicensePlate())
                .eq(StringUtils.isNotEmpty(jointCarRequest.getDriver()), JointCar::getDriver, jointCarRequest.getDriver())
                .eq(StringUtils.isNotEmpty(jointCarRequest.getPhoneNumber()), JointCar::getPhoneNumber, jointCarRequest.getPhoneNumber());
    }

}
